package io.renren.controller;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import io.renren.common.OurWxPayConfig;
import io.renren.common.QpayConstants;
import io.renren.common.WxpayParam;
import io.renren.utils.WXPayCommonUtil;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;


public class WxPayController {
    public static Map<String, String> wxPayFunction(HttpServletRequest request , WxpayParam wxpayParam)  throws Exception {
        //获取AppIP地址
        String ip = WXPayCommonUtil.getIp(request);
        OurWxPayConfig ourWxPayConfig = new OurWxPayConfig();
        WXPay wxPay = new WXPay(ourWxPayConfig);
        //根据微信支付api来设置
        //生成32位不重复的uuid
        String randomUUID = UUID.randomUUID().toString().replace("-","");
        System.out.println(randomUUID);
        SortedMap<String, String> signParams = new TreeMap<String, String>();
        //app_id
        signParams.put("appid", QpayConstants.WX_APP_ID);
        //商品参数信息
        signParams.put("body",wxpayParam.getBody());
        //微信商户账号
        signParams.put("mch_id", QpayConstants.WX_MCH_ID);
        //32位不重复的编号
        signParams.put("nonce_str", randomUUID);
        //回调地址
        signParams.put("notify_url", QpayConstants.WX_NOTIFYURL);
        //订单编号
        signParams.put("out_trade_no", wxpayParam.getOutTradeNo());
        //请求的实际ip地址
        signParams.put("spbill_create_ip",ip);
        //支付金额 单位为分
        signParams.put("total_fee",wxpayParam.getTotalPrice().multiply(new BigDecimal(100)).toBigInteger().toString());
        //付款类型为APP
        signParams.put("trade_type", "APP");
        //生成签名
        String sign = WXPayUtil.generateSignature(signParams, QpayConstants.API_KEY);
        signParams.put("sign", sign);
        //调用微信统一下单接口
        Map<String, String> respData = wxPay.unifiedOrder(signParams);
        if (respData.get("return_code").equals("SUCCESS")) {
            //返回给APP端的参数，APP端再调起支付接口
            Map<String, String> repData = new HashMap<>();
            //appId
            repData.put("appid", QpayConstants.WX_APP_ID);
            //商户号
            repData.put("partnerid", QpayConstants.WX_MCH_ID);
            //预付订单id
            repData.put("prepayid", respData.get("prepay_id"));
            //默认sign=WXPay
            repData.put("package", "Sign=WXPay");
            //自定义不重复的长度不长于32位
            repData.put("noncestr", respData.get("nonce_str"));
            //北京时间时间戳
            repData.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000) );
            //生成二次签名
            String signs = WXPayUtil.generateSignature(repData,QpayConstants.API_KEY);
            respData.put("sign", signs);
            respData.put("timestamp", repData.get("timestamp"));
            respData.put("package", "Sign=WXPay");
            return respData;
        }
        throw new Exception(respData.get("return_msg"));
    }
}